package com.example.esdemo.service;

import com.example.esdemo.entity.ESBook;
import org.springframework.data.elasticsearch.annotations.Highlight;
import org.springframework.data.elasticsearch.annotations.HighlightField;
import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("esBookRepository")
public interface EsBookRepository extends ElasticsearchRepository<ESBook, String> {
    //只需要根据业务的功能来取对应的方法名就可以了，具体的代码实现由框架来完成
    //类似于SpringDataJPA
    List<ESBook> findByTitleOrAuthor(String title,String author);

    /*
    {
       "match":{"title":"?0"}
    }
     */
    //高亮显示
    @Highlight(fields = {
            @HighlightField(name = "title"),
            @HighlightField(name = "author")
    })
    @Query("{\"match\":{\"title\":\"?0\"}}")
    SearchHit<ESBook> find(String keyword);
}
